package com.atguigu.user;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * Author:huzhongkui
 * Date: 2025-01-13 星期一 11:26:30
 * Description: 测试类
 */
public class ApiTest {


    public static void main(String[] args) {

        // 1 创建一个jwt的builder对象
        JwtBuilder builder = Jwts.builder();

        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "hzk");
        String jwt = builder.setClaims(map) // 载荷
                .setId(UUID.randomUUID().toString())// 唯一标识
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 3))
                .setHeaderParam("alg", "HS256")
                .setHeaderParam("typ", "JWT")
                .setSubject("测试jwt")
                .setIssuedAt(new Date())
                .setIssuer("hzk")
                .signWith(SignatureAlgorithm.HS256, "hzk".getBytes())
                .compact();

        System.out.println(jwt);

        parse();

    }

    private static void parse() {
        String jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiLmtYvor5Vqd3QiLCJuYW1lIjoiaHprIiwiaXNzIjoiaHprIiwiZXhwIjoxNzM2NzM5MzgzLCJpYXQiOjE3MzY3MzkzODAsImp0aSI6Ijg1NzUwMDQ3LWYwYjYtNGQ1NS1hNmUwLTQyYjVlOWM4MDM2OCJ9.RkD2HzXwm5o71xbp1I0Maqu2_DVPnPfzwz9e9hW4PNM";
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey("hzk".getBytes()).parseClaimsJws(jwt);

        Object name = claimsJws.getBody().get("name");
        System.out.println(name);
    }
}
